package com.elm.controller;

import com.elm.ElmSpringbootApplication;
import com.elm.common.ApiResponse;
import com.elm.po.OrderDetailet;
import com.elm.po.Orders;
import com.elm.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api")
public class OrdersController {

    private final ElmSpringbootApplication elmSpringbootApplication;
    @Autowired
    private OrdersService ordersService;

    OrdersController(ElmSpringbootApplication elmSpringbootApplication) {
        this.elmSpringbootApplication = elmSpringbootApplication;
    }

    @PostMapping("/orders")
    public ApiResponse<Orders> createOrders(@RequestBody Orders orders) {
    	try {
    		Orders createdorders = ordersService.createOrders(orders);
    		if(createdorders==null) {
    			return ApiResponse.error("500","创建订单失败");
    		}
    		return ApiResponse.success(createdorders);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }
    
 
    @GetMapping("/orders/{id}")
    public ApiResponse<Orders> getOrdersById(@PathVariable Integer id) {
    	try {
    		return ApiResponse.success(ordersService.getOrdersById(id));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }
    
    @GetMapping("/orders")
    public ApiResponse<List<Orders>> getOrderByUserId(@RequestParam Integer userId) {
    	try {
    		return ApiResponse.success( ordersService.getOrdersByUserId(userId));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @GetMapping("/orders/{userId}/user")
    public ApiResponse<List<Orders>> getOrdersByUserId(@PathVariable Integer userId) {
    	try {
    		return ApiResponse.success(ordersService.getOrdersByUserId(userId));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @PostMapping("/comments")
    public ApiResponse<Orders> addComment(@RequestBody Orders order) {
    	try {
    		Orders updatedOrders = ordersService.addComment(order);
    		if(updatedOrders==null) {
    			return ApiResponse.error("500","评论操作失败");
    		}
    		return ApiResponse.success(updatedOrders);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 

    }

    @GetMapping("/comments/{businessId}")
    public ApiResponse<List<Orders>> getAllCommentsByBusinessId(@PathVariable Integer businessId) {
    	try {
    		return ApiResponse.success(ordersService.getAllCommentsByBusinessId(businessId));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @GetMapping("/comments")
    public ApiResponse<List<Orders>> getAllComments() { 
    	try {
    		return ApiResponse.success(ordersService.getAllComments());
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @PutMapping("/comments/{orderId}/{result}")
    public ApiResponse<Integer> reviewComment(@PathVariable Integer orderId,@PathVariable Integer result) {
    	try {
    		Integer res =ordersService.reviewComment(orderId,result);
    		if(res==0) {
    			return ApiResponse.error("500","审核评论失败");
    		}
    		return ApiResponse.success(res);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }
 
    @GetMapping("/orders/{businessId}/business")
    public ApiResponse<List<Orders>> getAllOrdersByBusinessId(@PathVariable Integer businessId) {
    	try {
    		return ApiResponse.success(ordersService.getAllOrders(businessId));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @GetMapping("/orders/{businessId}/paid")
    public ApiResponse<List<Orders>> getAllPaidOrders(@PathVariable Integer businessId) {
    	try {
    		return ApiResponse.success(ordersService.getAllPaidOrders(businessId));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @GetMapping("/orders/{businessId}/ended")
    public ApiResponse<List<Orders>> getAllEndedOrders(@PathVariable Integer businessId) {
    	try {
    		return ApiResponse.success(ordersService.getAllEndedOrders(businessId));
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        } 
    }

    @PutMapping("/orders/{orderId}/{orderState}")
    public ApiResponse<Orders> updateOrders(@PathVariable Integer orderId,
                                            @PathVariable String orderState) {
        System.out.println("订单号： "+orderId);
        try {
//            System.out.println("订单id: "+orderId);
            Orders updatedorders = ordersService.handleOrders(orderId, orderState,1);
            if(updatedorders==null) {
                return ApiResponse.error("500","更新订单失败");
            }
            return ApiResponse.success(updatedorders);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }

    @PutMapping("/orders/pay/{orderId}/{paymentMethod}")
    public ApiResponse<Orders> userPay(@PathVariable Integer orderId,
                                       @PathVariable Integer paymentMethod) {
        System.out.println("订单号： "+orderId);
        try {
//            System.out.println("订单id: "+orderId);
            Orders updatedorders = ordersService.handleOrders(orderId,"paid",paymentMethod);
            if(updatedorders==null) {
                return ApiResponse.error("500","更新订单失败");
            }
            return ApiResponse.success(updatedorders);
        } catch (RuntimeException e) {
            return ApiResponse.error("400", e.getMessage());
        } catch (Exception e) {
            return ApiResponse.error("500", "系统错误：" + e.getMessage());
        }
    }


//    @GetMapping("/orderDetailet/{order}")
//    public ApiResponse<List<OrderDetailet>> getDetailetsByOrder(@PathVariable Integer order) {
//    	try {
//    		return ApiResponse.success(ordersService.getDetailetsByOrder(order));
//        } catch (RuntimeException e) {
//            return ApiResponse.error("400", e.getMessage());
//        } catch (Exception e) {
//            return ApiResponse.error("500", "系统错误：" + e.getMessage());
//        } 
//    }
}